package com.lan.code.swagger;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import org.apache.commons.lang.StringUtils;
import org.codehaus.groovy.util.StringUtil;

public class SwaggerMain extends AnAction {


    @Override
    public void actionPerformed(AnActionEvent e) {

        PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
        if (!(psiFile instanceof PsiJavaFile)) {
            return;
        }
        // 获取当前的project对象
        Project project = e.getProject();

        WriteCommandAction.runWriteCommandAction(project, () -> {

            //do something
            // 通过获取到PsiElementFactory来创建相应的Element，包括字段，方法，注解，类，内部类等等
            PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
            //导包
            PsiJavaFile file = (PsiJavaFile) psiFile;
            if (file.getImportList().findOnDemandImportStatement("io.swagger.annotations") == null
                    && file.getImportList().findSingleClassImportStatement("io.swagger.annotations.ApiModelProperty") == null) {
                file.getImportList().add(factory.createImportStatementOnDemand("io.swagger.annotations"));
            }
            // 获取class
            PsiClass psiClass = file.getClasses()[0];
            for (PsiField allField : psiClass.getFields()) {
//                System.out.printf("name:%s,text:%s", allField.getName(), allField.getText());
                //当前字段是否已引用该注解
                if (allField.getAnnotation("io.swagger.annotations.ApiModelProperty") != null) {
                    continue;
                }
//                allField.replace(factory.createFieldFromText("/** * a*/@TestAnnotation private String a;", allField));
                //有注释 私有方法
                String fieldText = allField.getText();
                if (!(fieldText.contains("/**"))) {
                    continue;
                }
                //注释内容
                String value = fieldText.substring(fieldText.indexOf("/**") + 3, fieldText.indexOf("*/"))
                        .replace("\n", "").replace("    ", "").replace("*", "").trim();
                if(StringUtils.isEmpty(value)){
                    continue;
                }
                PsiAnnotation annotation = factory.createAnnotationFromText("@ApiModelProperty(\"" + value + "\")\n\t", psiClass);
                PsiElement var2 = allField.getModifierList();
                if (var2.getChildren().length > 0) {
                    var2 = var2.getChildren()[0];
                }
//               添加
                allField.getModifierList().addBefore(annotation, var2);
                //整理代码
                CodeStyleManager.getInstance(project).reformat(psiClass);
//                StringBuilder fieldStr = new StringBuilder();
//                fieldStr.append(fieldText);
//                fieldStr.insert(fieldText.indexOf("private"), "@TestAnnotation(\"" + value + "\") \n\t");
//                //创建新字段插入
//                PsiElement nowField = factory.createFieldFromText(fieldStr.toString(), psiClass);
//                allField.delete();
//                psiClass.add(nowField);

            }
        });

    }
}
